#include <boost/test/unit_test.hpp>
#include <allegro.h>
#include "Midi.hpp"
#include "Keyboard.hpp"
#include <iostream>

BOOST_AUTO_TEST_CASE(ProceduralPlayMidi)
{
    const int LOOP = 1;
    const int NOLOOP = 0;

    allegro_init();		// equivalent to calling int install_allegro(int system_id, int *errno_ptr, int (*atexit_ptr)()); 
    install_keyboard();
    BOOST_REQUIRE_MESSAGE( install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL)==0, ("Can not install sound.") );
    BOOST_CHECK_MESSAGE( install_timer()==0, ("Can not install timer.") );

    {
        MIDI * midi = load_midi("../Midi/FF7aerit.mid");
        BOOST_CHECK_MESSAGE( midi, ("Could not load midi.") );
        BOOST_CHECK_MESSAGE( play_midi(midi, LOOP)==0, ("Can no play midi.") );
        rest(10000);
        destroy_midi(midi);     // not necessary to call stop_midi if we intend to destroy it
    }

    {
        MIDI * midi = load_midi("../Midi/FF7aerit.mid");
        BOOST_CHECK_MESSAGE( midi, ("Could not load midi.") );
        BOOST_CHECK_MESSAGE( play_midi(midi, LOOP)==0, ("Can not play midi.") );
        stop_midi();
        BOOST_CHECK_MESSAGE( play_midi(midi, LOOP)==0, ("Can not play midi.") );
        BOOST_CHECK_MESSAGE( play_midi(midi, LOOP)==0, ("Can not play midi.") );
        stop_midi();
        destroy_midi(midi);    
    }

    remove_timer();
    remove_sound();
    allegro_exit();
}

BOOST_AUTO_TEST_CASE(PlayMidi)
{
    {
        Midi m("../Midi/FF7aerit.mid");
        BOOST_CHECK_MESSAGE( install_timer()==0, ("Can not install timer.") );
        BOOST_REQUIRE_MESSAGE( m.Play(), "Can not play the midi." );

        rest(10000);
    }
    {
        Midi m("../Midi/FF7aerit.mid");
        BOOST_REQUIRE( m.IsReady() );
        BOOST_CHECK_MESSAGE( !m.Pause(), "Can not pause the midi." );
        BOOST_CHECK_MESSAGE( !m.Resume(), "Can not resume the midi." );
        BOOST_CHECK_MESSAGE( m.Play(), "Can not play the midi." );
        BOOST_CHECK_MESSAGE( !m.Play(), "Can not play the midi." );
        BOOST_CHECK_MESSAGE( !m.Loop(0,10), "Can not loop the midi." );
        BOOST_CHECK_MESSAGE( !m.Resume(), "Can not resume the midi." );
        BOOST_CHECK_MESSAGE( m.Pause(), "Can not pause." );
        BOOST_CHECK_MESSAGE( !m.Pause(), "Can not pause." );
        BOOST_CHECK_MESSAGE( !m.Play(), "Can not play the midi." );
        BOOST_CHECK_MESSAGE( !m.Loop(0,10), "Can not loop the midi." );
        BOOST_CHECK_MESSAGE( m.Resume(), "Can not resume." );
        BOOST_CHECK_MESSAGE( !m.Resume(), "Can not resume the midi." );
        BOOST_CHECK_MESSAGE( !m.Play(), "Can not play the midi." );
        BOOST_CHECK_MESSAGE( !m.Loop(0,10), "Can not loop the midi." );
        m.Stop();
        BOOST_CHECK_MESSAGE( !m.Pause(), "Can not pause the midi." );
        BOOST_CHECK_MESSAGE( !m.Resume(), "Can not resume the midi." );
    }
    {
        Midi m("../Midi/FF7aerit.mid");
        BOOST_REQUIRE( m.IsReady() );
        BOOST_CHECK_MESSAGE( !m.Loop(-10,10), "Can not loop the midi." );
        BOOST_CHECK_MESSAGE( m.Loop(0,10), "Can not loop the midi." );
        BOOST_CHECK_MESSAGE( !m.Loop(0,10), "Can not loop the midi." );
        BOOST_CHECK_MESSAGE( !m.Play(), "Can not play the midi." );
        BOOST_CHECK_MESSAGE( m.Pause(), "Can not pause." );
        BOOST_CHECK_MESSAGE( !m.Pause(), "Can not pause." );
        BOOST_CHECK_MESSAGE( !m.Play(), "Can not play the midi." );
        BOOST_CHECK_MESSAGE( !m.Loop(0,10), "Can not loop the midi." );
        BOOST_CHECK_MESSAGE( m.Resume(), "Can not resume." );
        BOOST_CHECK_MESSAGE( !m.Resume(), "Can not resume the midi." );
        BOOST_CHECK_MESSAGE( !m.Play(), "Can not play the midi." );
        BOOST_CHECK_MESSAGE( !m.Loop(0,10), "Can not loop the midi." );
        m.Stop();
        BOOST_CHECK_MESSAGE( !m.Loop(-10,10), "Can not loop the midi." );
        BOOST_CHECK_MESSAGE( m.Loop(0,10), "Can not loop the midi." );
        BOOST_CHECK_MESSAGE( !m.Loop(0,10), "Can not loop the midi." );
    }

    {
        //Midi m("../Midi/FF7aerit.mid");
        //BOOST_REQUIRE( m.IsReady() );
        //BOOST_CHECK_EQUAL( m.GetCurrentPosition(), 0 );
        //BOOST_CHECK( m.Seek(10) );
        //BOOST_CHECK( m.GetCurrentPosition()==10 );
        //BOOST_CHECK( m.Play() );
        //BOOST_CHECK( m.Seek(10) );
        //BOOST_CHECK( m.Seek(10) );
        //BOOST_CHECK( m.Seek(10) );
        //BOOST_CHECK( m.Pause() );
        //BOOST_CHECK( m.Seek(10) );
        //BOOST_CHECK( m.Seek(10) );
        //BOOST_CHECK( m.Seek(10) );
    }
}


